// pages/mine_problem/index.js
// 
const oss = require('../../../utils/oss.js')
const api = require('../../../utils/api').apis;
const mWx = require('../../../utils/request')
var arrurl =[]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value:'',
    num:0,
    pic:[],
    imgUrl:[],
    hidden:false
  },
  goods(){
    wx.navigateBack({
      delta: 1, // 返回上一级页面。
      success: function() {
          console.log('成功！')
      }
    })
  },
  getUserChat(aaaa,is){
    var _this = this
    if(this.data.imgUrl.length == this.data.pic.length ||is){
      console.log('图片上传')
      api.getCouple({
        content:this.data.value,
        imgUrl:this.data.pic
      }).then(res=>{
        console.log(res)
        if(res.success){
          _this.setData({
            hidden:true
          })
        } else{
          wx.showToast({
            title: '发布失败',
            icon:'none'
          })
          
        }
      })
    }
  },
  // 发布按钮事件提交
  publish(){
    var _this = this
    if(this.data.value){
      if(_this.data.imgUrl){
        this.getUserChat([],true)
      }
      for(let i=0;i<this.data.imgUrl.length;i++){
      api.getStsOss({}).then(res=>{
        if (res.success) {
          _this.upLoadFileService(this.data.imgUrl, res.data,i)
        }

      })
      }
      // if(_this.data.pic.length == 0){
      //   console.log('没有图片',_this.data.pic)
      //   _this.getUserChat(this.data.pic,true)
      // } else{
        
      // }
      console.log(this.data.pic)
      
    } else{
      wx.showToast({
            title: '内容不能为空',
            icon:'none'
          })
    }
  },
  // 图片删除
  deleteimg(e){
    // console.log(this.data.pic)
    let index = e.currentTarget.dataset.index
    
    console.log(index)
    // let pic = this.data.pic[index]
    let arr = this.data.imgUrl
    arr.splice(index,1)
    console.log(arr)
    // for(let i=0;i<=pic.length;i++){
    //   if(index == i){

    //   } else{
    //     arr.push(pic[i])
    //   }
    // }
    // console.log(arr)
    this.setData({
      imgUrl:arr
    })
  },
  // 图片上传
  choseImg: function () {
    var _this = this;
    wx.chooseImage({
      count: 9, //最多可以选择的图片总数
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        console.log(res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        let tempFilePaths = res.tempFilePaths;
        _this.setData({
          imgUrl:res.tempFilePaths
        })
        // console.log(_this.data.imgUrl)
        
      }
    });
  },
  upLoadFileService(file, ossData,i) {
    console.log(file,ossData)
    const that = this;
    const date = new Date()
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();
    let obj = oss.util.aliOssParams(ossData.tempAk, ossData.tempSk,'https://xuedaozaixain.oss-cn-beijing.aliyuncs.com');
    let alikey = `user/${year}${month}${day}/user_head_${date.getTime()}_${Math.round(Math.random() * 100)}.${file[i].split('.').pop()}`;
    wx.uploadFile({
      url: 'https://xuedaozaixain.oss-cn-beijing.aliyuncs.com',
      filePath: file[i],
      name: 'file',
      formData: {
        key: alikey,
        name: file[i],
        policy: obj.policy,
        OSSAccessKeyId: ossData.tempAk,
        success_action_status: '200',
        signature: obj.signature,
        'x-oss-security-token': ossData.token
      },
      success: (res) => {
        console.log("https://xuedaozaixain.oss-cn-beijing.aliyuncs.com" + "/" + alikey)

        arrurl.push('https://xuedaozaixain.oss-cn-beijing.aliyuncs.com' + "/" + alikey)
        console.log(arrurl)
        that.setData({
          pic: arrurl
        })
        that.getUserChat()
      },
      fail: function(res) {
        console.log(res)
        wx.hideToast();
        wx.showModal({
          title: '错误提示',
          content: '上传图片失败',
          showCancel: false,
          success: function(res) {}
        })
      }
    });
  },
  queryOssData() {
    return new Promise((resolve, reject) => {
      console.log(resolve)
      api.getStsOss({}).then(res=>{
        // resolve(res)
        console.log(res)
      })
      // mWx.request({
      //   url: getStsOss,
      //   success: (res) => {
      //     resolve(res)
      //   }
      // })
    })
  },
  // 
  bindinput(e){
    console.log(e)
    var value = e.detail.value
    // var len = parseInt(value.length);
    // if (num > value.lenght) return;
    var num = e.detail.cursor
    if(num == 200){
      wx.showToast({
        title: '您输入的字符已达上限',
        icon: 'none'
      })
    }
    this.setData({
      value:value,
      num:num
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})